package com.ctl.szmd.szmd.feature_wm.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.paging.LoadState
import com.ctl.lib_common.extensions.singleClick
import com.ctl.lib_common.widget.recyclerview.MyDecoration
import com.ctl.szmd.szmd.feature_wm.R
import com.ctl.szmd.szmd.feature_wm.data.model.WmDataListModel
import com.ctl.szmd.szmd.feature_wm.databinding.FragmentWmHomeBinding
import com.ctl.szmd.szmd.feature_wm.ui.adapter.footer.FooterAdapter
import com.ctl.szmd.szmd.feature_wm.ui.adapter.wm.WmBannerHomeAdapter
import com.ctl.szmd.szmd.feature_wm.ui.adapter.wm.WmHomeNewsAdapter
import com.ctl.szmd.szmd.feature_wm.ui.base.BaseWmBindingFragment
import com.ctl.szmd.szmd.feature_wm.ui.viewmodel.WmHomeViewModel
import com.google.android.material.appbar.AppBarLayout
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.fragment_wm_home.*
import kotlinx.android.synthetic.main.view_wm_tool_bar.*
import kotlinx.android.synthetic.main.wm_include_fragment_tab1_head.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collectLatest
import org.koin.android.viewmodel.ext.android.viewModel


class WmHomeFragment : BaseWmBindingFragment<FragmentWmHomeBinding>() {

    private val viewModel: WmHomeViewModel by viewModel()
    private val mAdapter by lazy { WmHomeNewsAdapter() }
    override fun getLayoutId(): Int {
        return R.layout.fragment_wm_home
    }

    @OptIn(ExperimentalCoroutinesApi::class)
    override fun initView() {
        binding.apply {
//            vm = viewModel
//            lifecycle.addObserver(viewModel)
            recyclerView.addItemDecoration(MyDecoration())
            recyclerView.adapter = mAdapter.withLoadStateFooter(FooterAdapter { mAdapter.retry() })
        }


        toolbar_title.text = "标题"
        refresh_layout.setOnRefreshListener {
            mAdapter.refresh()
            viewModel.getInfoWomenHeadlineListData()
        }
        head.addOnOffsetChangedListener(AppBarLayout.BaseOnOffsetChangedListener<AppBarLayout?> { _, verticalOffset ->
            refresh_layout.isEnabled = verticalOffset >= 0
        })

        mAdapter.listener = View.OnClickListener {
            if (it.tag is WmDataListModel) {
                val data = it.tag as WmDataListModel
                val bundle = Bundle()
                bundle.putString(WmWebFragment.TITLE, data.title)
                bundle.putString(WmWebFragment.URL, data.pageNetPath)
                bundle.putString(WmWebFragment.INFO_ID, data.id)
                bundle.putString(WmWebFragment.FROM_WHERE, "巾帼心向党")
                bundle.putInt(WmWebFragment.VISIT_TYPE_NUM, data.visitTypeNum ?: 0)
                bundle.putInt(WmWebFragment.ATTACHMENT_COUNT, data.attachmentCount ?: 0)
                findNavController().navigate(R.id.wmWebFragment, bundle)
            }
        }
        lifecycleScope.launchWhenCreated {
            mAdapter.loadStateFlow
                .collectLatest { state ->
                    refresh_layout.isRefreshing = state.refresh is LoadState.Loading
                }
        }
        viewModel.getInfoWomenHeadlineListData()
        viewModel.getInfoFacePartyListData().observe(viewLifecycleOwner, Observer {
            mAdapter.submitData(lifecycle, it)
        })

        banner.addBannerLifecycleObserver(this@WmHomeFragment)
            .setAdapter(
                WmBannerHomeAdapter(
                    mutableListOf()
                )
            ).indicator = CircleIndicator(requireContext())
        viewModel.mBannerListData.observe(viewLifecycleOwner, Observer {
            banner.adapter.setDatas(it)
            banner.adapter.notifyDataSetChanged()
            banner.setCurrentItem(1, false)
                .setIndicatorPageChange()
                .start()
        })

        banner.adapter.setOnBannerListener { data, _ ->
            if (data is WmDataListModel) {
                val bundle = Bundle()
                bundle.putString(WmWebFragment.TITLE, data.title)
                bundle.putString(WmWebFragment.URL, data.pageNetPath)
                bundle.putString(WmWebFragment.INFO_ID, data.id)
                bundle.putString(WmWebFragment.FROM_WHERE, "妇联头条")
                bundle.putInt(WmWebFragment.VISIT_TYPE_NUM, data.visitTypeNum ?: 0)
                bundle.putInt(WmWebFragment.ATTACHMENT_COUNT, data.attachmentCount ?: 0)
                findNavController().navigate(R.id.wmWebFragment, bundle)
            }
        }

        ll_fltt.singleClick {
            findNavController().navigate(R.id.wmHeadlinesFragment)
        }
        ll_jcfc.singleClick {
            findNavController().navigate(R.id.wmBasicStyleFragment)
        }
        ll_zjfl.singleClick {
            findNavController().navigate(R.id.walkIntoWmFragment)
        }
    }

    override fun onResume() {
        super.onResume()
        banner.start()
    }

    override fun onPause() {
        super.onPause()
        banner.stop()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        banner.destroy()
    }
}